package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.strategy.service.Strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 路由策略
 */
@Service(value = "router")
@Slf4j
public class RouterStrategy implements Strategy {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【策略模块】 路由策略开始执行！！！");
        //1. 封装队列的名称
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + submit.getGatewayID();

        //2. 创建指定的队列
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());

        //3. 发送消息到指定队列(使用默认的交换机)
        rabbitTemplate.convertAndSend(queueName,submit);

        //4. 记录日志,return true
        log.info("【策略模块】 路由策略执行成功！！！消息发送到了网关模块对应的队列");
        return true;
    }
}
